using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using RoboTrocity;

namespace RoboTrocity {
    class ObjectList {
        List<GameObject> objectArray;

        public ObjectList() {
            objectArray = new List<GameObject>();
        }

        public List<GameObject> ObjectArray {
            get {
                return objectArray;
            }
            set {
                objectArray = value;
            }
        }

        public void add(GameObject gObject){
            for (int i = 0; i < objectArray.Count-1; i++) {
                if (objectArray[i].Position.X < gObject.Position.X) {
                    objectArray.Insert(i + 1, gObject);
                    return;
                }
            }
            objectArray.Add(gObject);
        }

		public void remove(GameObject gObject) {
			objectArray.Remove(gObject);
		}

		public void sort() {  //I hate bubble sort.
            bool isSorted = false;
            while (!isSorted) {
                isSorted = true;
                for (int i = 0; i < objectArray.Count-1; i++) {
                    if (objectArray[i].Position.X > objectArray[i + 1].Position.X) {
                        GameObject temp = objectArray[i];
                        objectArray[i] = objectArray[i + 1];
                        objectArray[i + 1] = temp;
                        isSorted = false;
                    }
                }
            }
        }
    }
}



//for a LinkedList implementation
/*using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace RoboTrocity {
    class ObjectList {
		LinkedList<GameObject> objectCollection; 

        public ObjectList() {
            objectCollection = new LinkedList<GameObject>();
        }

        public LinkedList<GameObject> ObjectCollection {
            get {
                return objectCollection;
            }
            set {
                objectCollection = value;
            }
        }

        public void add(GameObject gObject){
			if (objectCollection.First = null) {
				objectCollection.AddFirst(gObject);
			}
			LinkedListNode<GameObject> listNode = objectCollection.First;
			while (listNode != null) {
				if (listNode.Value.Position.X > gObject.Position.X) {
					objectCollection.AddBefore(listNode, gObject);
					return;
				}
				listNode = listNode.Next;
			}

			objectCollection.AddLast(gObject);
        }

        public void sort() {  //sorts the linked list
            bool isSorted = false;
			LinkedListNode<GameObject> tempNode = null;
            while (!isSorted) {
                isSorted = true;
                foreach (LinkedListNode<GameObject> listNode in objectCollection) {
					if (listNode != objectCollection.Last){
						if (listNode.Value.Position.X > listNode.Next.Value.Position.X) {
							tempNode = listNode.Next;
							if (listNode != objectCollection.First) {
								listNode.Previous.Next = listNode.Next;
							}
							tempNode.Next.Previous = listNode;

							listNode.Next = tempNode.Next;
							tempNode.Previous = listNode.Previous;

							listNode.Previous = tempNode;
							tempNode.Next = listNode;
						}
					}
				}
            }
        }

		public void sortThisObject(GameObject gObject) {
			LinkedListNode<GameObject> objectNode = null;
			foreach (LinkedListNode<GameObject> listNode in objectCollection) {
				if (listNode.Value == gObject) {
					objectNode = listNode;
				}
				break;
			}
		}
    }
}
*/